package com.imddysc.dwz.utils;

import java.util.Stack;

/**
 * 十进制到六十二进制的转换工具 
 * @author xiongwei.dai
 *
 */
public class DecimalUtils {

	public static void main(String[] args) {
		System.out.println(36526445);
		System.out.println(_10_to_62(36526445));
		System.out.println(_62_to_10(_10_to_62(36526445)));
		System.out.println(_10_to_62(10));
		System.out.println(_10_to_62(100));
		System.out.println(_10_to_62(1000));
		System.out.println(_10_to_62(10000));
		
		System.out.println(_62_to_10("100000"));
		System.out.println(_10_to_62(916132832));
		System.out.println(_10_to_62(916133032L));
		
		System.out.println(_62_to_10("ZZZZZZ"));
		System.out.println(_62_to_10("ZZZZZY"));
		System.out.println(_10_to_62(56800235583L));
		System.out.println(_10_to_62(56800235582L));
		

		System.out.println("------------------------");

		
		System.out.println(_62_to_10("1000"));
		System.out.println(_10_to_62(238328L));
		
		System.out.println(_10_to_62(238428L));
		
		System.out.println(_62_to_10("ZZZZ"));
		System.out.println(_62_to_10("ZZZY"));
		System.out.println(_10_to_62(14776335L));
		System.out.println(_10_to_62(14776334L));
		
		
	}

	public static final char[] array = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
			'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

	public static String _10_to_62(long number) {
		Long rest = number;
		Stack<Character> stack = new Stack<Character>();
		StringBuilder result = new StringBuilder(0);
		while (rest != 0) {
			stack.add(array[new Long((rest - (rest / 62) * 62)).intValue()]);
			rest = rest / 62;
		}
		for (; !stack.isEmpty();) {
			result.append(stack.pop());
		}
		return result.toString();

	}

	public static long _62_to_10(String sixty_str) {
		long multiple = 1;
		long result = 0;
		Character c;
		for (int i = 0; i < sixty_str.length(); i++) {
			c = sixty_str.charAt(sixty_str.length() - i - 1);
			result += _62_value(c) * multiple;
			multiple = multiple * 62;
		}
		return result;
	}

	private static int _62_value(Character c) {
		for (int i = 0; i < array.length; i++) {
			if (c == array[i]) {
				return i;
			}
		}
		return -1;
	}

	
	
}
